package com.df.happylearn.model;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;

public class ArticleContentInfo implements Serializable {
    public int mId;
    public int mUserId;
    public String mViewName;
    public String mTitle;
    public String mContent;
    public String mViews;
    public String mCtime;
    public String mCatName;
    public String mThumbnail;
    public String mFromUrl;

    //json对象解析
    public static ArticleContentInfo parseJson(JSONObject jsonObject) {
        try {
            ArticleContentInfo item = new ArticleContentInfo();
            if (jsonObject.has("id")) {
                item.mId = Integer.parseInt(jsonObject.getString("id"));
            }
            if (jsonObject.has("user_id")) {
                item.mUserId = Integer.parseInt(jsonObject.getString("user_id"));
            }
            if (jsonObject.has("view_name")) {
                item.mViewName = jsonObject.getString("view_name");
            }
            if (jsonObject.has("title")) {
                item.mTitle = jsonObject.getString("title");
            }
            if (jsonObject.has("content")) {
                item.mContent = jsonObject.getString("content");
            }
            if (jsonObject.has("views")) {
                item.mViews = jsonObject.getString("views");
            }
            if (jsonObject.has("ctime")) {
                item.mCtime = jsonObject.getString("ctime");
            }
            if (jsonObject.has("cat_name")) {
                item.mCatName = jsonObject.getString("cat_name");
            }
            if (jsonObject.has("thumbnail")) {
                item.mThumbnail = jsonObject.getString("thumbnail");
            }
            if (jsonObject.has("from_url")) {
                item.mFromUrl = jsonObject.getString("from_url");
            }
            return item;
        } catch (Exception e) {
            Log.i("fsljdfksd", "parseJson error:" + e.toString());
            return null;
        }
    }

    //jsonArray对象解析
    public static ArrayList<ArticleContentInfo> parseJsonArray(JSONArray jsonArray) {
        try {
            ArrayList<ArticleContentInfo> itemList = new ArrayList<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                ArticleContentInfo item = ArticleContentInfo.parseJson(jsonArray.getJSONObject(i));
                if (item != null) {
                    itemList.add(item);
                }
            }
            return itemList;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
}
